home *** CD-ROM | disk | FTP | other *** search
/ Aminet 12 / Aminet 12 (1996)(GTI - Schatztruhe)[!][Jun 1996].iso / Aminet / dev / e / framework.lha / fw / storable.e < prev    next >
Encoding:
Text File  |  1996-01-28  |  2.5 KB  |  87 lines

  1.  
  2. -> Object persistency in E 3.1a  (may be crash with higher versions!!)
  3. -> It is quite limited but it works!!
  4. -> Persistency is usefull and allows you to dynamically store and load
  5. -> entire polymorphic object structures. It can be used in OO databases
  6. -> or OO compilers in which it provides a lazy way for module precompilation.
  7. -> It will often avoid the need to write these ugly load/save procedures.
  8.  
  9. -> Copyright © Guichard Damien 01/04/1996
  10.  
  11. OPT MODULE
  12. OPT EXPORT
  13. OPT NOWARN
  14.  
  15. MODULE 'fw/any'
  16.  
  17. DEF file
  18.  
  19. -> A storable object without memory space penalty.
  20. -> All collection classes inherit from it so all collections are persistent.
  21. OBJECT storable OF any
  22. ENDOBJECT
  23.  
  24. -> Set storage file for persistency.
  25. -> storage MUST be a valid file-handle.
  26. PROC setStorage(storage) OF storable
  27.   file:=storage
  28. ENDPROC
  29.  
  30. -> Get the storage file.
  31. -> setStorage MUST have been called before.
  32. PROC storage() OF storable IS file
  33.  
  34. -> Load an entire polymorphic structure from storage file.
  35. -> setStorage MUST have been called before with a READ mode file
  36. -> which has been previously produced by a VALID storeObject() call.
  37. PROC loadObject() OF storable
  38.   DEF class=0
  39.   Fread(file,{class},SIZEOF LONG,1)
  40.   MOVE.L  A4, D0
  41.   ADD.L   D0, class
  42.   self:=FastNew(^class)
  43.   ^self:=class
  44.   Fread(file,self+SIZEOF LONG,^class-SIZEOF LONG,1)
  45.   self.load()
  46. ENDPROC self
  47.  
  48. -> Store an entire polymorphic structure to storage file.
  49. -> setStorage MUST have been called before with a WRITE mode file.
  50. -> Currently there is strong limitations about object structures that
  51. -> can be stored: there MUST be no cycles and multi-pointed object
  52. -> will be duplicated as no mark is left on objects. Moreover object MUST not
  53. -> be CHIP-RAM-only as there is no guarantee that it will re-loaded in CHIP.
  54. PROC storeObject() OF storable
  55.   DEF class
  56.   MOVE.L  A4, class
  57.   class:=^self-class
  58.   Fwrite(file,{class},4,1)
  59.   Fwrite(file,self+SIZEOF LONG,Long(^self)-SIZEOF LONG,1)
  60.   self.store()
  61. ENDPROC
  62.  
  63. -> Load a string from storage file.
  64. PROC loadString() OF storable
  65.   DEF size=0
  66.   DEF str
  67.   Fread(file,{size},SIZEOF LONG,1)
  68.   str:=New(size+1)
  69.   Fread(file,str,size,1)
  70. ENDPROC str
  71.  
  72. -> Store a string to storage file.
  73. -> BEWARE, E strings are turned into simple strings.
  74. PROC storeString(str:PTR TO CHAR) OF storable
  75.   DEF size
  76.   size:=StrLen(str)
  77.   Fwrite(file,{size},SIZEOF LONG,1)
  78.   Fwrite(file,str,size,1)
  79. ENDPROC
  80.  
  81. -> Explain how to load this object.
  82. PROC load() OF storable IS EMPTY
  83.  
  84. -> Explain how to store this object.
  85. PROC store() OF storable IS EMPTY
  86.  
  87.